
//
// 120325 - AH - (Bybanen *) Created.
// 120618 - AH - (Huset *) Took in use CE_ID HttpRequestParameter.
//

package org.soekrat.server.servlets.ce;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.japril.servlet.HttpRequestDef;
import org.soekrat.server.io.db.ConversationElement;
import org.soekrat.server.io.db.SoekratDAO;
import org.soekrat.server.io.db.SoekratDAOFactory;
import org.soekrat.server.servlets.AbstractSoekratHttpServlet;

/**
 * <p>
 *   Implements request to delete a
 *   {@link ConversationElement}.
 * </p>
 * @author Arne Halvorsen (AH)
 * @author Laetitia Le Chatton (LLC)
 */
@SuppressWarnings("serial")
public final class DeleteConversationElementHttpServlet
  extends AbstractSoekratHttpServlet
{
  /**
   * <p>
   *   Constructor.
   * </p>
   */
  public DeleteConversationElementHttpServlet(){}

  @Override
  protected void handleRequest(HttpServletRequest req, HttpServletResponse res)
    throws ServletException, IOException 
  {
    HttpRequestDef def=getHttpRequestDef();
    
    if (!hasParameters(req))
    {
      PrintWriter pw=printSimpleHtmlStart(res);
      pw.println(def.compileApiHtml());
      printSimpleHtmlEnd(pw);
    }
    else
    {
      if (!validate(res, def, req)) return;
      
      long key=def.fetch(CE_ID.name(), req).getLongValue();
      
      SoekratDAO dao=new SoekratDAOFactory().create();
      if (dao.deleteConversationElement(key))
      {
        respond(res, "ok");
      }
      else
      {
        respond(res, "ConversationElement for key: "+key+" not found"); 
      }
    }
  }
  
  @Override
  public HttpRequestDef getHttpRequestDef()
  {
    HttpRequestDef retVal=new HttpRequestDef("/soekrat_server/delete_ce")
      .desc("Delete Conversation Element");
    retVal.parameter(CE_ID);
    return retVal;
  }
  
}
